package com.jnu.keepaccounts.utls;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.jnu.keepaccounts.R;

public class KeyBoardUtils {
    private int flag = -1;
    private KeyboardView keyboardView;
    private EditText editText;
    private final Keyboard k1;//自定义键盘
    public interface OnEnsureListener{
        public void onEnsure();
        public void setYuSuan();
    }
    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public KeyBoardUtils(KeyboardView keyboardView, EditText editText){
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘
        k1 = new Keyboard(this.editText.getContext(), R.xml.keyboard)    ;

        this.keyboardView.setKeyboard(k1);
        this.keyboardView.setEnabled(true);
        this.keyboardView.setPreviewEnabled(false);
        this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮被点击了的监听

        //init();
    }
    private void init() {
        Paint payPaint = new Paint();
        payPaint.setTextAlign(Paint.Align.CENTER);
//        payPaint.setTextSize(ScreenUtils.sp2px(getContext(), 24));
        payPaint.setColor(Color.BLUE);
        payPaint.setAntiAlias(true);
    }


    KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {

        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (primaryCode){
                case Keyboard.KEYCODE_DELETE://点击了删除
                    if(editable!= null&&editable.length()>0){
                        if(start>0){
                            editable.delete(start-1,start);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_DONE://点击了保存
                    onEnsureListener.onEnsure();//通过接口回调的方法，当点击保存时，可以调用这个方法
                    break;
                case -3://点击了计入预算
//                    flag = -flag;
                    onEnsureListener.setYuSuan();
                    break;
                default:
                    editable.insert(start,Character.toString((char) primaryCode));
                    break;
            }
        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };


    //显示键盘
    public void showKeyboad(){
        int visibility = keyboardView.getVisibility();
        if(visibility == View.INVISIBLE||visibility == View.GONE){
            keyboardView.setVisibility(View.VISIBLE);
        }
    }
    //隐藏键盘
    public void hideKeyboard(){
        int visibility = keyboardView.getVisibility();
        if(visibility == View.VISIBLE||visibility== View.INVISIBLE){
            keyboardView.setVisibility(View.GONE);
        }
    }




}
